package utils.db.impl;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pojo.EDeviceState;
import pojo.EDeviceType;
import utils.db.api.IEditDevice;

public class EditDevice extends DBOperation implements IEditDevice {
	private static Logger logger = LoggerFactory.getLogger(EditDevice.class);

	public EditDevice() {
		super();
	}

	@Override
	public void complexUpdateDevice(Integer id, Integer clientId,
			Integer servicemanId, EDeviceType type, String desc) {
		changeClientId(id, clientId);
		changeServicemanId(id, servicemanId);
		changeDescription(id, desc);
		changeType(id, type);

	}

	@Override
	public void changeStatus(Integer id, EDeviceState state) {
		executeQuery(prepareUpdateQuery("state", state.name(), "id",
				String.valueOf(id)));
	}

	@Override
	public void changeDescription(Integer id, String desc) {
		executeQuery(prepareUpdateQuery("description", desc, "id",
				String.valueOf(id)));
	}

	@Override
	public void changeType(Integer id, EDeviceType type) {
		executeQuery(prepareUpdateQuery("type", type.name(), "id",
				String.valueOf(id)));
	}

	@Override
	public void changeClientId(Integer id, Integer clientId) {
		executeQuery(prepareUpdateQuery("client_id", String.valueOf(clientId),
				"id", String.valueOf(id)));
	}

	@Override
	public void changeServicemanId(Integer id, Integer servicemanId) {
		executeQuery(prepareUpdateQuery("serviceman_id",
				String.valueOf(servicemanId), "id", String.valueOf(id)));

	}

	@Override
	public void deleteDevice(int id) {
		try {
			initializeDeleteDeviceStatement(id, DELETE_DEVICE);
		} catch (Exception e) {
			logger.error("Error during delete device by query " + DELETE_DEVICE);
		}
	}

	@Override
	public void deleteDeviceByClientID(Integer id) {
		try {
			initializeDeleteDeviceStatement(id, DELETE_DEVICE_BY_CLIENT_ID);
		} catch (Exception e) {
			logger.error("Error during delete device by query "
					+ DELETE_DEVICE_BY_CLIENT_ID);
		}
	}

	private void executeQuery(String query) {
		try {
			pstmt = super.getConnector().prepareStatement(query);
			pstmt.execute();
			pstmt.close();
		} catch (SQLException e) {
			logger.error("Error during executing query " + query);
		}
		logger.info("Executed query " + query);
	}

	private String prepareUpdateQuery(String column, String value,
			String conditionColumn, String param) {
		StringBuilder sbuild = new StringBuilder();
		sbuild.append("update devices set ").append(column).append("= ")
				.append("'" + value + "'").append(" where ")
				.append(conditionColumn).append(" = ").append(param)
				.append(";");
		return sbuild.toString();
	}

	private void initializeDeleteDeviceStatement(int id, String query)
			throws SQLException {
		pstmt = super.getConnector().prepareStatement(query);
		pstmt.setInt(1, id);
		pstmt.execute();
		pstmt.close();
	}

	public static void main(String[] args) {
		IEditDevice editDevice = new EditDevice();
		editDevice.complexUpdateDevice(8, 98, 97, EDeviceType.OTHER, "myszka swieci w nocy na roz");
	}

}
